/*
 * @Author: wangyongcun
 * @Date: 2023-09-06 10:50:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-06-10 10:32:12
 * @Description: 成果库管理
 */
class Fruit {
  constructor() {}
  /**
   * @description: 添加技术成果
   * @param {*} ctx
   * @return {*}
   */
  async addFruit(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(
        ["title", "field", "agency", "conversion", "stage", "content"],
        ctx
      );
      if (isEmpty) return;
      // 查询数据是否已存在
      // 添加到数据库
      let rowBody = ctx.request.body;
      let rowBodyKeys = Object.keys(rowBody);
      let sqlParams = `id='${uuidv4()}',`;
      rowBodyKeys.forEach((x, i) => {
        sqlParams += `${x}='${rowBody[x]}'${
          i == rowBodyKeys.length - 1 ? "" : ","
        }`;
      });
      let sql = `INSERT INTO fruit SET ${sqlParams}`;
      let { status } = await db(sql);
      if (status != 200) throw "添加科技成果失败";
      // 返回结果
      ctx.body = resUtil.success("添加成功");
    } catch (err) {
      log.error(`Fruit-addFruit-添加科技成果失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 更新技术成果
   * @param {*} ctx
   * @return {*}
   */
  async updateFruit(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["id"], ctx);
      if (isEmpty) return;
      // 查询数据是否已存在
      // 添加到数据库
      let rowBody = ctx.request.body;
      if (!rowBody.auditStatus) {
        rowBody.auditStatus = 0;
        rowBody.auditDesc = "";
      }
      let rowBodyKeys = Object.keys(rowBody);
      let sqlParams = "";
      rowBodyKeys.forEach((x, i) => {
        sqlParams += `${x}='${rowBody[x]}'${
          i == rowBodyKeys.length - 1 ? "" : ","
        }`;
      });
      let sql = `UPDATE fruit SET ${sqlParams} WHERE id='${rowBody.id}'`;
      let { status, results } = await db(sql);
      if (status != 200 || results.affectedRows == 0) throw "更新失败";
      // 返回结果
      ctx.body = resUtil.success("更新成功");
    } catch (err) {
      log.error(`Fruit-updateFruit-更新失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 删除技术成果
   * @param {*} ctx
   * @return {*}
   */
  async deleteFruit(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["id"], ctx);
      if (isEmpty) return;
      // 添加到数据库
      let rowBody = ctx.request.body;
      let sql = `DELETE FROM fruit WHERE id='${rowBody.id}'`;
      let { status, results } = await db(sql);
      if (status != 200 || results.affectedRows == 0) throw "未找到该科技成果";
      // 返回结果
      ctx.body = resUtil.success("删除成功");
    } catch (err) {
      log.error(`Fruit-deleteFruit-删除失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 查询技术成果
   * @param {*} ctx
   * @return {*}
   */
  async queryFruit(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["id"], ctx);
      if (isEmpty) return;
      // 添加到数据库
      let rowBody = ctx.request.body;
      let sql = `SELECT * FROM fruit WHERE id='${rowBody.id}'`;
      let { status, results } = await db(sql);
      console.log(results, "results");
      if (status != 200 || results.length == 0) throw "未找到该科技成果";
      // 返回结果
      ctx.body = resUtil.success({ Content: results[0] });
    } catch (err) {
      log.error(`Fruit-queryFruit-查询失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
  /**
   * @description: 查询科技成果列表
   * @param {*} ctx
   * @return {*}
   */
  async queryFruitList(ctx) {
    try {
      // 获取参数
      let isEmpty = reqUtil.isEmpty(["pageNum", "pageSize"], ctx);
      if (isEmpty) return;
      let { pageNum, pageSize, keyword, submitterId, auditStatus } =
        ctx.request.body;
      let whereSql = keyword ? `WHERE title LIKE '%${keyword}%'` : "";
      if (submitterId) {
        whereSql += `${
          whereSql ? "AND" : "WHERE"
        } submitterId='${submitterId}'`;
      }
      if (auditStatus) {
        whereSql += `${
          whereSql ? "AND" : "WHERE"
        } auditStatus='${auditStatus}'`;
      }
      // 查询总条数
      let Count = 0;
      let countSql = `SELECT COUNT(1) FROM fruit ${whereSql}`;
      let { status: countStatus, results: countRes } = await db(countSql);
      if (countStatus != 200) throw "查询总条数失败";
      Count = countRes[0]["COUNT(1)"];
      // 查询列表数据
      let listSql = `SELECT * FROM fruit ${whereSql} ORDER BY updateTime DESC LIMIT ${pageSize} OFFSET ${
        pageSize * (pageNum - 1)
      }`;
      let { status: listStatus, results: listRes } = await db(listSql);
      if (listStatus != 200) throw "查询科技成果列表失败";
      // 返回结果
      ctx.body = resUtil.success({ Content: listRes, Count });
    } catch (err) {
      log.error(`Fruit-queryFruitList-查询失败：${err}`);
      ctx.body = resUtil.error(err);
    }
  }
}

export default new Fruit();
